package com.zhiwang.planet.mvvm.binding

import androidx.databinding.*
import com.zhiwang.planet.widget.TagLayout



object TagLayoutBindingAdapter {


    @JvmStatic
    @BindingAdapter("tags")
    fun bindTabLayout(tagLayout: TagLayout, tags: List<String>) {

        tagLayout.setTags(tags)
    }


    @JvmStatic
    @BindingAdapter("tag")
    fun bindOneTabLayout(tagLayout: TagLayout, tag:String) {

        tagLayout.setTags(listOf(tag))
    }

    @JvmStatic
    @BindingAdapter("checkable_tags")
    fun bindTabLayoutForCheableTags(tagLayout: TagLayout, tags: List<String>) {
        tagLayout.setCheckableTags(tags)
    }

    @JvmStatic
    @InverseBindingAdapter(attribute = "checkedTag")
    fun getCheckedTag(tagLayout: TagLayout): String {

        return tagLayout.getCheckedText().toString()
    }


    @JvmStatic
    @BindingAdapter("checkedTag")
    fun setCheckedTag(tagLayout: TagLayout, tag: String?) {
//        tagLayout.check()
        //not support ,we just need one-way binding ,but adapt android data-binding to retrieve checked tag
    }


    @JvmStatic
    @BindingAdapter(value = ["onCheckedChange", "checkedTagAttrChanged"], requireAll = false)
    fun bindingTagLayoutChange(tag: TagLayout, listener: TagLayout.OnCheckedChangeListener?, attrChange: InverseBindingListener?) {

        if (attrChange == null)
            tag.checkedChangeListener = listener
        else tag.checkedChangeListener = object : TagLayout.OnCheckedChangeListener {
            override fun onCheckedChange(checkedIndex: Int, prevCheckedIndex: Int) {
                listener?.onCheckedChange(checkedIndex, prevCheckedIndex)
                attrChange.onChange()
            }

        }
    }


}